import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class Main {
    public static Frame.Label mainLabel;
    public static Tools tools = new Tools();
    public static int i = 0;
    public static void main(String[] args) {
        Frame frame_1 = new Frame(400,200);
        Frame.Label label_1 = frame_1.addLabel("按空格开始测定BPM",25);
        Frame.Label label_2 = frame_1.addLabel("在重拍按下空格，按下退格键即可重新测定",13);
        Frame.Label label_3 = frame_1.addLabel("通常情况下，BPM都是整数，小数点后面的数值仅供参考",13);
        Main.mainLabel = label_1;
        KeyAdapter space = new SpaceActive();
        frame_1.addKeyListener(space);
    }
    public static class SpaceActive extends KeyAdapter {
        long pressTimeA,pressTimeB;
        @Override
        public void keyPressed(KeyEvent e) {
            float c = 0;
            if (e.getKeyCode() == KeyEvent.VK_SPACE){
                i++;
                pressTimeA=System.nanoTime();
                if (pressTimeB != 0) {
                    c = passTime(pressTimeA, pressTimeB);
                }
                Tools.passTime = Tools.passTime+c;
                pressTimeB=pressTimeA;
                if (i<10){
                    Main.mainLabel.setText("还要再按"+(10-i)+"次");
                }
                else {
                    Main.mainLabel.setText("BPM=" + tools.bpmCounter());
                }
            }
            else if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE){
                i=0;
                pressTimeA=0;
                pressTimeB=0;
                Tools.passTime=0;
                Main.mainLabel.setText("重新测定");
            }
        }
        float passTime(long A,long B){
            float c;
            c=(float)((A-B)/100000)/10000;
            return c;
        }
    }

}
